﻿using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Windows.Forms;

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private const string ApiBaseUrl = "http://localhost:5044";

        private void txtName_TextChanged(object? sender, EventArgs e)
        {
        }

        private async void btnCreateCard_Click(object? sender, EventArgs e)
        {
            btnCreateCard.Enabled = false;
            try
            {
                var request = new AddPatientRequest
                {
                    PatientName = txtName.Text?.Trim(),
                    IdCard = txtIdCard.Text?.Trim(),
                    Age = int.TryParse(txtAge.Text?.Trim(), out var ageVal) ? ageVal : null,
                    Phone = txtPhone.Text?.Trim(),
                    Ethnicity = txtEthnicity.Text?.Trim(),
                    Educationlevel = txtEducation.Text?.Trim(),
                    Occupation = txtOccupation.Text?.Trim(),
                    Postalcode = txtPostal.Text?.Trim(),
                    Maritalstatus = cbMarital.SelectedItem?.ToString(),
                    Gender = rbMale.Checked,
                    Birthday = dpBirthday.Value
                };

                using var http = new HttpClient { BaseAddress = new Uri(ApiBaseUrl) };
                var json = JsonSerializer.Serialize(request);
                using var content = new StringContent(json, Encoding.UTF8, "application/json");
                var response = await http.PostAsync("api/Patient/AddPatient", content);
                response.EnsureSuccessStatusCode();
                var respJson = await response.Content.ReadAsStringAsync();
                var apiResult = JsonSerializer.Deserialize<ApiResultPatient>(respJson, new JsonSerializerOptions
                {
                    PropertyNameCaseInsensitive = true
                });

                if (apiResult != null && apiResult.Code == 100)
                {
                    string cardNumber = apiResult.Data?.CardNumber ?? "未知";
                    using var dialog = new CardNumberDialog(cardNumber);
                    dialog.ShowDialog(this);
                    // 关闭医疗卡办理窗口
                    this.Close();
                }
                else
                {
                    MessageBox.Show($"办理失败：{apiResult?.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"请求失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                btnCreateCard.Enabled = true;
            }
        }

        private class AddPatientRequest
        {
            public string? PatientName { get; set; }
            public string? IdCard { get; set; }
            public int? Age { get; set; }
            public string? Phone { get; set; }
            public string? Ethnicity { get; set; }
            public string? Educationlevel { get; set; }
            public string? Occupation { get; set; }
            public string? Postalcode { get; set; }
            public string? Maritalstatus { get; set; }
            public bool Gender { get; set; }
            public DateTime? Birthday { get; set; }
        }

        private class ApiResultPatient
        {
            public int Code { get; set; }
            public string? Message { get; set; }
            public PatientResultData? Data { get; set; }
        }

        private class PatientResultData
        {
            public int PatientId { get; set; }
            public string? CardNumber { get; set; }
        }
    }
}
